#include "helpers.h"

int ffs(unsigned int word) {
    int bit = 0;

	if (word)
	{
		/* Clear out all but LSB bit */
		word &= ~word + 1;
		if (word & 0xffff0000) bit += 16;
		if (word & 0xff00ff00) bit += 8;
		if (word & 0xf0f0f0f0) bit += 4;
		if (word & 0xcccccccc) bit += 2;
		if (word & 0xaaaaaaaa) bit += 1;
		bit++;
	}

	return bit-1;
}

int fls(unsigned int word) {
	int bit = 32;

	if (!word) bit -= 1;
	if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }
	if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }
	if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }
	if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }
	if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }

	return bit-1;
}
